/*******************************************************************************
 * Copyright (c) 2010, 2030 www.itlaobing.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *******************************************************************************/
package cn.itlaobing.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.itlaobing.entity.DtoEntity;
import cn.itlaobing.entity.UserEntity;
import cn.itlaobing.exception.BusinessException;
import cn.itlaobing.services.DtoService;
import cn.itlaobing.services.GoodsService;
import cn.itlaobing.services.impl.UserServiceimpl;
import cn.itlaobing.web.annotation.MappingAnnotation;
import net.sf.json.JSONObject;

/**
 * ClassName: MappingServlet <br/>
 * Function:重写serlvet类 <br/>
 * date: 2017年10月31日 下午7:26:17 <br/>
 * 
 * @author 王博
 * @version
 * @since JDK 1.8
 */
@WebServlet("/servlet/*")
public class MappingServlet extends BaseServlet {

	/**
	 * add:(增加方法). <br/>
	 * 
	 * @author 王博
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws ServletException 
	 * @since JDK 1.8
	 */
	@MappingAnnotation("/login")
	public void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		request.setCharacterEncoding("utf-8");
		String name=request.getParameter("username");
		String pwd=request.getParameter("password");
		
		//创建正则表达式
		//验证用户名
		String reg="^[A-Za-z_][\\w]{5,10}$";
		//验证手机号码
		String reg1="^[1-9][0-9]{10}$";
		if(!(name.matches(reg) || name.matches(reg1))) {
			request.setAttribute("ERRORMESSAGE", "100");
			System.out.println("用户名登录失败");
			request.getRequestDispatcher("/login.jsp").forward(request, response);
			return;
		}
		//验证密码
		if(pwd.length()<6 || pwd.length()>10) {
			request.setAttribute("ERRORMESSAGE", "100");
			System.out.println("密码登录失败");
			response.sendRedirect("/login.jsp");
			return;
		}
		UserServiceimpl us=new UserServiceimpl();
		UserEntity ue=null;
		try {
			ue=us.login(name, pwd);
		} catch (BusinessException e) {
			request.setAttribute("ERRORMESSAGE", "100");
			System.out.println("查询登录失败");
			request.getRequestDispatcher("/login.jsp").forward(request, response);
			return;
		}
		//验证改用户是否为空
		if(ue==null) {
			request.setAttribute("ERRORMESSAGE", "100");
			System.out.println("为空登录失败");
			request.getRequestDispatcher("/login.jsp").forward(request, response);
			return;
		}
		//修改用户登录时间
		int row=0;
		try {
			row = us.updatePlaceOrder(new Date(), ue.getId());
		} catch (BusinessException e) {
			row=0;
		}
		if(row<0) {
			System.out.println("修改上次登录时间出错");
		}
		//将用户存入session中
		request.getSession().setAttribute("USER", ue);
		queryAll(request,response);
	}
	
	/** 
	 * queryAll:(查詢). <br/>
	 * 
	 * @author 王博 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException 
	 * @since JDK 1.8 
	 */  
	@MappingAnnotation("/queryAll")
	public void queryAll(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		UserEntity ue=(UserEntity) request.getSession().getAttribute("USER");
		DtoService ds=new DtoService();
		List<DtoEntity> listdto=null;
		try {
			listdto=ds.query(ue.getId());
		} catch (BusinessException e) {
			listdto=null;
		}
		request.getSession().setAttribute("DTOQUERY", listdto);
		request.getRequestDispatcher("/WEB-INF/page/index.jsp").forward(request, response);
	}
	
	/** 
	 * updateStatus:(更改订单状态). <br/>
	 * 
	 * @author 王博 
	 * @param request
	 * @param response
	 * @throws UnsupportedEncodingException 
	 * @since JDK 1.8 
	 */  
	@MappingAnnotation("/updateStatus")
	public void updateStatus(HttpServletRequest request, HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		request.setCharacterEncoding("utf-8");
		String id=request.getParameter("id");
		String stat=request.getParameter("stat");
		GoodsService gs=new GoodsService();
		int row=0;
		try {
			row=gs.updateStatusId(Integer.valueOf(id),Integer.valueOf(stat));
		} catch (BusinessException e) {
			row=0;
		}
		//编写json数据
		JSONObject json= JSONObject.fromObject("{result:"+row+"}");
		out.print(json.toString());
		out.flush();
		out.close();
	}
}
